package org.jeecg.modules.business.repertory.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.business.repertory.entity.WmsRepertory;
import org.jeecg.modules.business.repertory.entity.WmsRepertoryFreezeHistory;
import org.jeecg.modules.business.repertory.entity.vo.RepertoryAreaVO;
import org.jeecg.modules.business.repertory.entity.vo.RepertoryDocumentVO;
import org.jeecg.modules.business.repertory.entity.vo.RepertoryLocationVO;
import org.jeecg.modules.business.repertory.entity.vo.RepertoryVO;
import org.jeecg.modules.business.repertory.entity.vo.param.RepertoryParamVO;
import org.jeecg.modules.business.repertory.service.IWmsRepertoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 库存
 * @Author: jeecg-boot
 * @Date: 2023-07-31
 * @Version: V1.0
 */
@Api(tags = "库存")
@RestController
@RequestMapping("/repertory/wmsRepertory")
@Slf4j
public class WmsRepertoryController extends JeecgController<WmsRepertory, IWmsRepertoryService> {
    @Autowired
    private IWmsRepertoryService wmsRepertoryService;

    /**
     * 库存-查询全部库存
     *
     * @return org.jeecg.common.api.vo.Result<java.util.List < org.jeecg.modules.business.repertory.entity.vo.RepertoryVO>>
     * @author hansen
     * @since 2023/10/10 9:40
     */
    @AutoLog(value = "库存-查询全部库存")
    @ApiOperation(value = "库存-查询全部库存", notes = "库存-查询全部库存")
    @GetMapping(value = "/all")
    public Result<List<RepertoryVO>> all() {
        List<RepertoryVO> repertorys = wmsRepertoryService.all();
        return Result.ok(repertorys);
    }

    /**
     * 库存-入库提交
     *
     * @param id
     * @return org.jeecg.common.api.vo.Result<java.lang.Void>
     * @author hansen
     * @since 2023/9/19 9:11
     */
    @AutoLog(value = "库存-入库提交")
    @ApiOperation(value = "库存-入库提交", notes = "库存-入库提交")
    @GetMapping(value = "/in/submit")
    public Result<List<WmsRepertory>> inSubmit(String id) {
        List<WmsRepertory> wmsRepertories = wmsRepertoryService.inSubmit(id);
        return Result.ok(wmsRepertories);
    }

    /**
     * 库存-移库提交
     *
     * @param id
     * @return org.jeecg.common.api.vo.Result<java.lang.Void>
     * @author hansen
     * @since 2023/8/31 13:21
     */
    @AutoLog(value = "库存-移库提交")
    @ApiOperation(value = "库存-移库提交", notes = "库存-移库提交")
    @GetMapping(value = "/move/submit")
    public Result<Void> moveSubmit(String id) {
        wmsRepertoryService.moveSubmit(id);
        return Result.ok();
    }

    /**
     * 库存-出库提交
     *
     * @param id
     * @return org.jeecg.common.api.vo.Result<java.lang.Void>
     * @author hansen
     * @since 2023/8/31 13:20
     */
    @AutoLog(value = "库存-出库提交")
    @ApiOperation(value = "库存-出库提交", notes = "库存-出库提交")
    @GetMapping(value = "/out/submit")
    public Result<Void> outSubmit(String id) {
        wmsRepertoryService.outSubmit(id, true);
        return Result.ok();
    }

    /**
     * 分页列表查询
     *
     * @param param
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "库存-分页列表查询")
    @ApiOperation(value = "库存-分页列表查询", notes = "库存-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<RepertoryVO>> queryPageList(RepertoryParamVO param,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Result<IPage<RepertoryVO>> result = new Result<>();
        Page<RepertoryVO> pageList = new Page<>(pageNo, pageSize);
        IPage<RepertoryVO> res = wmsRepertoryService.listByPage(pageList, param);
        //辅数量无值不返回
        res.getRecords().forEach(one -> {
            if (ObjectUtil.isNotNull(one.getUsableQuantitySub())) {
                if (one.getUsableQuantitySub().compareTo(BigDecimal.ZERO) == 0) {
                    one.setUsableQuantitySub(null);
                }
            }
            if (ObjectUtil.isNotNull(one.getStockQuantitySub())) {
                if (one.getStockQuantitySub().compareTo(BigDecimal.ZERO) == 0) {
                    one.setStockQuantitySub(null);
                }
            }
            if (Objects.isNull(one.getStockAge()) && !Objects.isNull(one.getCreateTime())) {
                //计算库龄
                one.setStockAge(new BigDecimal(DateUtil.between(new Date(), one.getCreateTime(), DateUnit.HOUR)));
            }
        });
        result.setSuccess(true);
        result.setResult(res);
        return result;
    }

    /**
     * 库存-锁定
     *
     * @param wmsRepertory
     * @return
     */
    @AutoLog(value = "库存-锁定")
    @ApiOperation(value = "库存-锁定", notes = "库存-锁定")
    @RequiresPermissions("repertory:wms_repertory:lock")
    @PostMapping(value = "/lock")
    public Result<String> lock(@RequestBody WmsRepertory wmsRepertory) {
        wmsRepertoryService.lock(wmsRepertory);
        return Result.OK("锁定成功!");
    }

    /**
     * 库存-解锁
     *
     * @param wmsRepertory
     * @return
     */
    @AutoLog(value = "库存-解锁")
    @ApiOperation(value = "库存-解锁", notes = "库存-解锁")
    @RequiresPermissions("repertory:wms_repertory:unlock")
    @PostMapping(value = "/unlock")
    public Result<String> unlock(@RequestBody WmsRepertory wmsRepertory) {
        wmsRepertoryService.unlock(wmsRepertory);
        return Result.OK("解锁成功!");
    }

    /**
     * 库存-冻结
     *
     * @param wmsRepertoryFreezeHistory
     * @return
     */
    @AutoLog(value = "库存-冻结")
    @ApiOperation(value = "库存-冻结", notes = "库存-冻结")
    @RequiresPermissions("repertory:wms_repertory:freeze")
    @PostMapping(value = "/freeze")
    public Result<String> freeze(@RequestBody WmsRepertoryFreezeHistory wmsRepertoryFreezeHistory) {
        wmsRepertoryService.freeze(wmsRepertoryFreezeHistory);
        return Result.OK("冻结成功!");
    }

    /**
     * 库存-解冻
     *
     * @param wmsRepertoryFreezeHistory
     * @return
     */
    @AutoLog(value = "库存-解冻")
    @ApiOperation(value = "库存-解冻", notes = "库存-解冻")
    @RequiresPermissions("repertory:wms_repertory:unfreeze")
    @PostMapping(value = "/unfreeze")
    public Result<String> unfreeze(@RequestBody WmsRepertoryFreezeHistory wmsRepertoryFreezeHistory) {
        wmsRepertoryService.unfreeze(wmsRepertoryFreezeHistory);
        return Result.OK("解冻成功!");
    }

    /**
     * 通过id查询获取库区信息
     *
     * @param id
     * @return List<RepertoryAreaVO>
     */
    @AutoLog(value = "库存-通过id查询库区信息")
    @ApiOperation(value = "库存-通过id查询库区信息", notes = "库存-通过id查询库区信息")
    @GetMapping(value = "/getAreaById")
    public Result<List<RepertoryAreaVO>> getAreaById(@RequestParam(name = "id", required = true) String id) {
        return Result.ok(wmsRepertoryService.getAreaById(id));
    }

    /**
     * 通过id查询获取库位信息
     *
     * @param id
     * @return List<RepertoryLocationVO>
     */
    @AutoLog(value = "库存-通过id查询库位信息")
    @ApiOperation(value = "库存-通过id查询库位信息", notes = "库存-通过id查询库位信息")
    @GetMapping(value = "/getLocationById")
    public Result<List<RepertoryLocationVO>> getLocationById(@RequestParam(name = "id", required = true) String id) {
        return Result.OK(wmsRepertoryService.getLocationById(id));
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "库存-通过id查询")
    @ApiOperation(value = "库存-通过id查询", notes = "库存-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<org.jeecg.modules.business.repertory.entity.vo.RepertoryVO> queryById(@RequestParam(name = "id", required = true) String id) {
        return Result.OK(wmsRepertoryService.queryById(id));
    }

    /**
     * 库存-增加库存
     *
     * @param repertoryDocumentVOS
     * @return org.jeecg.common.api.vo.Result<java.lang.Void>
     * @author hansen
     * @since 2023/8/18 8:08
     */
//    @AutoLog(value = "库存-增加库存")
//    @ApiOperation(value = "库存-增加库存", notes = "库存-增加库存")
//    @PostMapping(value = "/add")
//    public Result<Void> add(@RequestBody RepertoryDocumentVO repertoryDocumentVOS) {
//        wmsRepertoryService.add(repertoryDocumentVOS);
//        return Result.OK("新增成功！");
//    }

    /**
     * 库存-减少库存
     *
     * @param repertoryDocumentVOS
     * @return org.jeecg.common.api.vo.Result<java.lang.Void>
     * @author hansen
     * @since 2023/8/18 8:09
     */
    @AutoLog(value = "库存-减少库存")
    @ApiOperation(value = "库存-减少库存", notes = "库存-减少库存")
    @PostMapping(value = "/reduce")
    public Result<Void> reduce(@RequestBody RepertoryDocumentVO repertoryDocumentVOS) {
        wmsRepertoryService.reduce(repertoryDocumentVOS);
        return Result.OK("减少成功！");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "即时库存管理-通过excel导入数据", notes = "即时库存管理-通过excel导入数据")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, String code) {
        return wmsRepertoryService.importExcel(request, code);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param wmsRepertory
     */
    @ApiOperation(value = "即时库存管理-导出数据", notes = "即时库存管理-导出数据")
    @RequiresPermissions("repertory:wms_repertory:export")
    @PostMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WmsRepertory wmsRepertory) {
        return super.exportXls(request, wmsRepertory, WmsRepertory.class, "即时库存");
    }

}
